const { HttpException } = require("../core/http-exception");

const catchError = async (ctx, next) => {
  try {
    await next();
  } catch (error) {
    const isDev = global.environment === 'dev'
    const isHpptException = error instanceof HttpException
    if(isDev && !isHpptException){
      throw error
    }
    if (isHpptException) {
      ctx.body = {
        msg: error.msg,
        errorCode: error.errorCode,
        success:error.success,
        path: `${ctx.method} ${ctx.path}`,
      };
      ctx.status = error.code;
    } else {
      ctx.body = {
        msg: "o(╥﹏╥)o",
        success:error.success,
        errorCode: 999,
        path: `${ctx.method} ${ctx.path}`,
      };
      ctx.status = 500;
    }
  }
};
module.exports = catchError;
